package com.eazy.lkv2.web.controller.system;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.eazy.lkv2.core.web.utils.AddrUtils;
import com.eazy.lkv2.core.web.utils.StrKit;
import com.eazy.lkv2.web.controller.BaseController;
import com.eazy.lkv2.web.entity.LoginLog;
import com.eazy.lkv2.web.service.system.SystemLogService;
import com.eazy.lkv2.web.service.system.SystemPermissionService;
import com.eazy.lkv2.web.service.system.count.BaiduService;
import com.google.common.collect.Maps;

/**
 * 首页展示
 * 
 * @date 2016.06.16
 */
@Controller
@RequestMapping("system")
public class SystemIndexController extends BaseController<Object,Integer> {

	@Autowired
	private SystemLogService systemLogService;
	@Autowired
	private BaiduService baiduService;
	@Autowired
	private SystemPermissionService permissionService;

	/**
	 * 跳转到首页
	 */
	@RequiresAuthentication
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView login() {
		// 缓存菜单信息
		List<Map<String, Object>> data = null;
		if(existsCatch("leftMenu")) {
			String catchData = getCatch("leftMenu");
			data = toGson(catchData);
		} else {
			data = permissionService.permissionList(getCurrentUser().getId().toString());
			String catchData = toJson(data);
			putCatch("leftMenu", catchData , 60 * 3); // 3分钟后重置数据
		}
		addObject("data", data);
		return getSuccess("index/index");
	}

	/**
	 * 跳转到欢迎页
	 */
	@RequiresAuthentication
	@RequestMapping(value = "/welcome", method = RequestMethod.GET)
	public ModelAndView welcome() {
		Map<String, Object> view = Maps.newHashMap();

		List<Map<String, Object>> data = null;

		if (existsCatch("welcome")) {
			String catchData = getCatch("welcome");
			data = toGson(catchData);
		} else {
			data = systemLogService.select();
			// 2016-09-09 13:57:22 新加入功能
			for(Map<String,Object> map : data) {
				map.put("city", "");
				if(StrKit.notEmpty(map.get("IP")+"")) {
					List<LoginLog> login_log = baiduService.queryByObject(new LoginLog(map.get("IP")+""));
					if(!login_log.isEmpty())
						map.put("city", login_log.get(0).getCity());
				} 
			}
			String catchData = toJson(data);
			putCatch("welcome", catchData , 60 * 3); // 3分钟后重置数据
		}
		view.put("data", data);
		view.put("count", data.size());
		view.put("ip", "");
		view.put("last_time", "");

		if (data != null) {
			view.put("ip", data.get(0).get("IP"));
			view.put("last_time", data.get(0).get("tim"));
		}

		//2016-7-21 增加您的登录次数
		view.put("refCount", 0);
		
		/**
		 * 2016-7-7 新加入天气功能
		 */
		final String ipAddr = getIPaddr();
		
		/**
		 * 可以根据ip地址反查经纬度信息，地区信息，等等此处在1.0 MQ处有应用 但对于这个项目只需要拿出地区信息就好
		 */
		if (!ipAddr.equals("127.0.0.1")) {
			// 2016-7-15 先去login_log 表查看是否存在数据，避免多次请求
			List<LoginLog> loginLog = baiduService.queryByObject(new LoginLog(ipAddr));
			
			view.put("refCount", loginLog.size());
			
			String cityName = "";
			if(!loginLog.isEmpty())
				cityName = loginLog.get(0).getCity();
			else
				cityName = AddrUtils.getCity(ipAddr);
			
			// 2016-8-20 新加入此功能
			Object value = getAttrSession(ipAddr);
			JSONObject weatherData = null;
			if(value != null) {
				weatherData =  (JSONObject) value;
				removeAttrSession(ipAddr);
			} else {
				weatherData = getWeather(cityName);
			}
			
			view.put("cityName", cityName);
			view.put("temperature", weatherData.getString("temperature")); // 温度
			view.put("weather", weatherData.getString("weather")); // 天气状况
			view.put("wind", weatherData.getString("wind")); // 天气状况
			
			// 天气图片 我就当 18：00 是晚上了。。。。 否则是白天
			if (StrKit.getHour().equals("18")) {
				view.put("pictureUrl", weatherData.getString("nightPictureUrl"));
			} else {
				view.put("pictureUrl", weatherData.getString("dayPictureUrl"));
			}
		}
		
		addAllObjects(view);
		return getSuccess("index/welcome");
	}
}
